﻿
@{
    ViewBag.Title = "不良原因配置";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">

                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.BadType">不良大类：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtType" placeholder="请输入分类代码或名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.BadReason">不良原因：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtReason" placeholder="请输入原因代码或名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <button class="layui-btn layui-btn-normal layuiadmin-btn-list" type="button" onclick="edit(0);">
                            <i class="layui-icon layui-icon-addition layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer'], function () {
                var layer = layui.layer,
                    table = layui.table;

                var options = {
                    elem: '#table'
                    , url: '/ParaSetting/QueryBadReasons'
                    , method: "post"
                    , where: { type: '', reason: '' }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers", width: 60 }
                        , { field: 'BadTypeCode', title: '大类代码', width: 100 }
                        , { field: 'BadTypeName', title: '大类名称' }
                        , {
                            title: '来料不良', width: 100, templet: function (d) {
                                return d.IsMaterielBad ? "是" : "否";
                            } }
                        , { field: 'ReasonCode', title: '不良代码', width: 100 }
                        , { field: 'ReasonCN', title: '不良原因（中）' }
                        , { field: 'ReasonVN', title: '不良原因（越）' }
                        , { field: 'ReasonEN', title: '不良原因（英）' }
                        , {
                            title: '操作', width: 200, fixed: 'right', templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-normal" onclick="edit(' + d.PkId + ')"><i class="layui-icon">&#xe642;</i>编辑</button>\
                                           <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="del(' + d.PkId + ',\'' + d.ReasonCN + '\')"><i class="layui-icon">&#xe640;</i>删除</button>';
                                return str;
                            }
                        }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    options.where.type = $("#txtType").val().trim();
                    options.where.reason = $("#txtReason").val().trim();
                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }
            });
        }

        function edit(pkId) {
            layer.open({
                type: 2,
                shade: [0.1, '#000'],
                area: ['550px', '600px'],
                content: "/ParaSetting/BadReasonEdit?pkId=" + pkId,
                end: function () {
                    $("#btnSearch").click();
                }
            });
        }

        function del(pkId, name) {
            layer.confirm('确定要删除“' + name + '”吗？', { icon: 3, title: '提示' }, function (index) {
                var loadId = layer.load();
                $.ajax({
                    url: "/ParaSetting/BadReasonDelete?pkId=" + pkId,
                    complete: function () {
                        layer.close(loadId);
                    },
                    success: function (res) {
                        if (res.ErrCode != 200) {
                            layer.msg(res.ErrMsg, { icon: 5 });
                            return;
                        }
                        layer.msg("删除成功！", { icon: 6, time: 1500 });
                        $("#btnSearch").click();
                    }
                });
                layer.close(index);
            });
        }


    </script>
}
